package com.yan.demo4;

import com.alibaba.fastjson.JSON;
import com.yan.demo.Student;
import com.yan.demo2.Book;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.sql.ClientInfoStatus;
import java.util.*;

/**
 * @author taoge
 * @Date 2024/8/14 16:40
 * @Description
 */
public class ReflectDemo4 {
    // 将对象转为JSON
    @Test
    public  void test1() {
        // 1. 将对象转为json格式
        Book book = new Book("水浒传" , "yft" , 19.9 , new Date());
        System.out.println(book);
        String bookStr = MyJson.toJsonString(book);
        System.out.println("bookStr = " + bookStr);
    }

    // 将Object集合对象转为JSON
    @Test
    public void test2(){
        List<Book> list = new LinkedList<>();
        list.add(new Book("水浒传" , "yft" , 19.9 , new Date()));
        list.add(new Book("三国演义" , "wl" , 29.9 , new Date()));
        String jsonArrayString = MyJson.toJsonArrayString(list);
        System.out.println("jsonArrayString = " + jsonArrayString);
    }

    @Test
    public void test3(){
        List<Book> list = new LinkedList<>();
        list.add(new Book("水浒传" , "yft" , 19.9 , new Date()));
        list.add(new Book("三国演义" , "wl" , 29.9 , new Date()));
        Book book = new Book("红楼梦", "hpy", 39.9, new Date());
        System.out.println(MyJson.objectToJsonString(book));
        System.out.println(MyJson.objectToJsonString(list));

        // MyJson.objectToJsonString(new HashMap<>());
    }

    @Test
    /*  有问题
        对象套对象转字符串
        对象套对象截取
     */
    public void test4() throws Exception {
        String str = "{\"name\":\"红楼梦\",\"author\":\"hpy\",\"price\":39.9,\"createDate\":1723634340004}";
        Book book = MyJson.jsonStringToDomain(str, Book.class);
        System.out.println(book);

        Student stu1 = new Student("yft", 18);
        String s = JSON.toJSONString(stu1);
        Student newStudent = MyJson.jsonStringToDomain(s, Student.class);
        System.out.println("newStudent = " + newStudent);
         /*  Book book = MyJson.jsonStringToDomain(str, Book.class);
        System.out.println("book = " + book);*/

         /*  Student student1 = new Student("yft", 18, new Book("红楼梦", "hpyhpy", 39.9, new Date()));
        String test1 = "{\"name\":\"yft\",\"age\":18}";
        String str1 = MyJson.objectToJsonString(student1); //str1 = {"age":18,"book":{"author":"hpyhpy","createDate":1723683154194,"name":"红楼梦","price":39.9},"name":"yft"}
        System.out.println("str1 = " + str1);*/

         //Student student = MyJson.jsonStringToDomain(str1, Student.class);
        //System.out.println("student = " + student);

         /* Book book =new Book("红楼梦", "hpyhpy", 39.9, new Date());
        String s = MyJson.objectToJsonString(book);
        System.out.println("s = " + s);*/

         /*Student student = JSON.parseObject(str1, Student.class);
        System.out.println(student);
        */

         /*Class c = Student.class;
        Field[] declaredFields = c.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField.getGenericType().getTypeName());
        }*/
    }

    @Test
    public void test5(){
        LinkedList<Book> books = new LinkedList<>();
        books.add(new Book("三国演义1" , "yft1" , 29.9 , new Date()));
        books.add(new Book("三国演义2" , "yft2" , 39.9 , new Date()));
        books.add(new Book("三国演义3" , "yft3" , 49.9 , new Date()));
        String s = JSON.toJSONString(books);
        System.out.println("s = " + s);

        Collection<Book> books1 = MyJson.JsonStringArrayToDomain(s, Book.class);
        books1.forEach (item-> System.out.println(item));

    }

}
